package com.kcwl.carrier.domain.service;

import com.kcwl.carrier.domain.entity.CarrierGroupInfo;
import com.kcwl.carrier.domain.entity.GroupMappingInfo;
import com.kcwl.carrier.domain.repository.ICarrierGroupRepository;
import com.kcwl.carrier.domain.repository.IGroupMappingRepository;
import com.kcwl.carrier.infrastructure.repository.po.CarrierGroupPO;
import com.kcwl.carrier.infrastructure.repository.po.GroupMappingPO;
import com.kcwl.carrier.interfaces.command.CarrierGroupAddCommand;
import com.kcwl.carrier.interfaces.command.CarrierGroupUpdateCommand;
import com.kcwl.carrier.interfaces.dto.CarrierGroupInfoDTO;
import com.kcwl.carrier.interfaces.query.IdQuery;
import com.kcwl.ddd.infrastructure.utils.KcPageConverter;
import com.kcwl.ddd.interfaces.dto.PageInfoDTO;
import com.kcwl.framework.utils.KcBeanConverter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author healen
 */
@Slf4j
@Service
public class CarrierGroupDomainService {

    @Resource
    private ICarrierGroupRepository groupRepository;
    @Resource
    private IGroupMappingRepository mappingRepository;

    public void addGroup(CarrierGroupAddCommand groupAddCommand) {
        CarrierGroupPO groupPO = KcBeanConverter.toBean(groupAddCommand, CarrierGroupPO.class);
        Date time = new Date();
        groupPO.setCreateTime(time);
        groupPO.setUpdateTime(time);
        groupRepository.save(groupPO);
    }

    public void updateGroup(CarrierGroupUpdateCommand groupUpdateCommand) {
        CarrierGroupPO groupPO = KcBeanConverter.toBean(groupUpdateCommand, CarrierGroupPO.class);
        groupPO.setUpdateTime(new Date());
        groupRepository.updateById(groupPO);
    }

    public void deleteGroup(Long id) {
        groupRepository.removeById(id);
    }

    public CarrierGroupInfo groupInfo(Long id) {
        return KcBeanConverter.toBean(groupRepository.getById(id), CarrierGroupInfo.class);
    }

    public GroupMappingInfo getMappingInfo(Long mappingId, Long groupId) {
        return KcBeanConverter.toBean(mappingRepository.getMappingInfo(mappingId, groupId), GroupMappingInfo.class);
    }

    public void addGroupMapping(Long mappingId, Long groupId, Long userId) {
        GroupMappingPO mappingPO = new GroupMappingPO();
        mappingPO.setMappingId(mappingId);
        mappingPO.setGroupId(groupId);
        mappingPO.setCreateId(userId);
        Date time = new Date();
        mappingPO.setCreateTime(time);
        mappingPO.setUpdateTime(time);
        mappingRepository.save(mappingPO);
    }

    public PageInfoDTO<CarrierGroupInfoDTO> groupList(IdQuery query) {
        return KcPageConverter.toPageInfoDTO(groupRepository.groupList(query), CarrierGroupInfoDTO.class);
    }

    public void deleteGroupMapping(Long id) {
        mappingRepository.removeById(id);
    }

    /**
     * 通过 分组id获取列表
     */
    public List<GroupMappingInfo> queryMappingByGroupIdAndUser(Long groupId, Long userId){
        return KcBeanConverter.toList(mappingRepository.queryByGroupIdAndUserId(groupId,userId),GroupMappingInfo.class);
    }
}
